package com.wangfei.lecture.shell;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ShellExe {
    public static void main(String[] args) throws IOException {
//        ProcessBuilder processBuilder = new ProcessBuilder("ping www.baidu.com", "./");
//        try {
//            Process process = processBuilder.start();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(
//                    process.getInputStream()));
//            String line;
//            while ((line = reader.readLine()) != null) {
//                System.out.println(line);
//            }
//            reader.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        BufferedReader reader = null;
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("traceroute 127.1.0.1");
            reader=new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            String line;
            while((line = reader.readLine()) != null) {
                if(line.startsWith(" 2")) {
                    reader.close();
                    process.destroy();
                    break;
                } else {
                    System.out.println(line);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader != null) {
                reader.close();
            }
            if(process != null) {
                process.destroy();
            }
        }
    }
}
